package logic

import (
	"context"
	"errors"
	"yim_server/yim_file/file_rpc/types/file_rpc"

	"yim_server/yim_file/file_api/internal/svc"
	"yim_server/yim_file/file_api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ImageUploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

type FileData struct {
	FileName      string `json:"fileName"`
	FileType      string `json:"fileType"`
	FileUrl       string `json:"fileUrl"`
	FileHash      string `json:"fileHash"`
	FileSize      int64  `json:"fileSize"`
	FileExtension string `json:"fileExtension"`
}

func NewImageUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageUploadLogic {
	return &ImageUploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ImageUploadLogic) ImageUpload(req *types.ImageRequest, fileData *FileData) (resp *types.ImageResponse, err error) {
	_, err = l.svcCtx.FileRpc.UploadFile(context.Background(), &file_rpc.UploadFileRequest{
		Name:      fileData.FileName,
		Url:       fileData.FileUrl,
		Hash:      fileData.FileHash,
		Extension: fileData.FileExtension,
		Size:      fileData.FileSize,
		Type:      fileData.FileType,
		UserId:    uint32(req.UserID),
	})
	if err != nil {
		logx.Error(err)
		return nil, errors.New(err.Error())
	}

	resp = new(types.ImageResponse)
	return
}
